var client = require("../DAOImpl/mongodb");
var dataConfig = require("../config/database.json");
var host = dataConfig.host;
var database = dataConfig.db;
var db = new client(host, database);
var status = require("../config/status.json");
var notification = require('../private/notification');

var currentLat = 31.2051659;
var currentLng = 121.5854;
/*
 * @path /getDriverPosition
 * @req.body: dri_id cus_id 
 * for example: {
 * 'statusCode' : '0', 'driverId' : '1', 'currentLat' : currentLat, 'currentLng' :
 * currentLng, 'distance' : '200', 'saveTime' : '2' };
 */
function getDriverPosition(req, res, next) {
	console.log('getDriverPosition');
	
	var result = new Object();

	// 1.find the driver info
	db.findOne(dataConfig.col_driver, {"_id": req.body.dri_id}, function(err1, err2, doc_dri){
		if(err1 || err2){
			result.status = status.ERROR.VALUE;
			res.send(result);
		}else{
			// 2.find the customer info
			db.findOne(dataConfig.col_customer, {"_id": req.body.dri_id}, function(err1, err2, doc_cus){
				if(err1 || err2){
					result.status = status.ERROR.VALUE;
					res.send(result);
				}else{
					var locations = new Object();
					locations.destination = doc_dri.latitude + ',' + doc_dri_longitude;
					locations.origin = doc_cus.latitude + ',' + doc_dri_longitude;
					// 3.get the distance between driver and customer
					require("../private/google/service").getDistance(
							locations,
							function(err, data_map) {
								if (!err) {
									result.status = status.SUCCESS.VALUE;
									result.dri_info = new Object();
									result.dri_info.id = doc_dri._id;
									result.dri_info.lat = doc_dri.lat;
									result.dri_info.lng = doc_dri.lng;
									result.distance = data_map.distance;
									result.saveTime = data_map.duration;
									
									res.send(result);
								} else {
									result.status = status.ERROR.VALUE;
									res.send(result);
								}
							});//end of 3
				}
			});//end of 2
		}
	});//end of 1

	return next();
}

/*
 * @path /acceptDriver
 * @req.body:dri_id, cus_id The customer accept the request which is sent from
 * the driver who wants to accept the order.
 * The message which is sent to the driver contains order_id order_status
 */
function acceptDriver(req, res, next) {
	var result = new Object();
	var selector = new Object();
	var document = new Object();
	var selector_order = new Object();
	
	selector.driver_id = req.body.dri_id;
	selector.customer_id = req.body.cus_id;
	selector.stauts = {$ne:status.ORDERSTATUS.CUSTOMERACCEPT.VALUE};
	
	selector_order.cus_id = req.body.cus_id;

	//1.find the order message 
	db.findOne(dataConfig.col_order, selector, function(err1, err2, doc1){
		if(err1 || err2){
			result.status = status.SYSTEMERROR.VALUE;
			res.send(result);
		}else{
			var message = new Object();
			message.order_id = doc1._id;
			message.order_status = status.ORDERSTATUS.CUSTOMERACCEPT.VALUE;
			//2.send message to the driver to tell him the order is finished.
			var notifi = new notification(req.body.dri_id, message, function(err3, req, res, data) {
				if(err3){
					result.status = status.SYSTEMERROR.VALUE;
					res.send(result);
				}else{
					//3.find the driver info
					db.findOne(dataConfig.col_driver, {"dri_id": req.body.dri_id}, function(err1, err2, doc_dri){
						if(err1 || err2){
							result.status = status.ERROR.VALUE;
							res.send(result);
						}else{	
							//4.find the customer info
							db.findOne(dataConfig.col_customer, {"cus_id": req.body.dri_id}, function(err1, err2, doc_cus){
								if(err1 || err2){			
									result.status = status.ERROR.VALUE;
									res.send(result);
								}else{
									var locations = new Object();
									locations.destination = doc_dri.latitude + ',' + doc_dri_longitude;
									locations.origin = doc_cus.latitude + ',' + doc_dri_longitude;
									//5.get the distance and duration
									require("../private/google/service").getDistance(
											locations,
											function(err, data_map) {
												if (!err) {
													db.update(dataConfig.col_order, selector, document,
															function(err1, err2, doc_update) {
																if (err1 || err2) {
																	result.status = status.ERROR.VALUE;
																	res.send(result);
																}else{
																	result.driver_id = req.body.driver_id;
																	result.current_lat = doc_dri.lat;
																	result.current_lng = doc_dri.lng;
																	result.distance = data_map.distance.text;
																	result.saveTime = data_map.duration.text;
																	result.status = status.SUCCESS.VALUE;

																	res.send(result);
																} 
															});
												} else {
													result.status = status.ERROR.VALUE;
													res.send(result);
												}
											});// end of 5
								}
							});//end of 4
						}
					});//end of 3
				}
			});	//end of 2
		}
	});//end of 1
	return next();
}


/*
 * @path /refusedri
 * @req.body:cus_id, dri_id {'cus_id': '1','dri_id':'2'}
 */
function refuseDriver(req, res, next) {
	var result = new Object();
	var selector = new Object();
	var document = new Obejct();

	selector.cus_id = req.body.cus_id;
	selector.dri_id = req.body.dri_id;
	selector.stauts = {$ne:status.ORDERSTATUS.CUSTOMERACCEPT.VALUE};
	document.status = status.ORDERSTATUS.REFUSEDIRVER.VALUE;

	var message = new Object();
	
	db.update(dataConfig.col_order, selector, document, function(err2, err3, doc) {
		if (err2 || err3) {
			result.status = status.SYSTEMERROR.VALUE;
			result.err = err2 + ' ' + err3;
			res.send(result);
		}else {
			message.order_id = doc._id;
			message.order_status = doc.status;
			var notifi = new notification(req.body.dri_id, message, function(err1, req, res, data) {
				if(err1){
					result.status = status.SYSTEMERROR.VALUE;
					res.send(result);
				}else{
					result.status = status.SUCCESS.VALUE;
					res.send(result);
				}
			});
		}
	});
	
	return next;
}

/*
 * @path /assdirver
 * @req.body:order_id, result {'order_id': '1', 'result':'5'}
 */
function assessDriver(req, res, next) {
	var result = new Object();
	var selector = new Object();
	var document = new Obejct();

	selector.order_id = req.body.order_id;
	selector.result = req.body.result;
	document.status = status.EVALUATION[req.body.result - 1].VALUE;

	db.update(dataConfig.col_order, selector, document, function(err1, err2, doc) {
		if (err1 || err2){
			result.status = status.SYSTEMERROR.VALUE;
			res.send(result);
		}else{
			result.status = status.SUCCESS.VALUE;
			res.send(result);
		}  
	});
	return next;
}
exports.services = [ getDriverPosition,  refuseDriver,
		acceptDriver, assessDriver ];